package com.view.Buying.com.com.forestandthetrees.controls.adgFooter
{
import flash.events.MouseEvent;

import mx.controls.AdvancedDataGrid;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.EventPriority;
import mx.events.AdvancedDataGridEvent;

public class FooterAdvancedDataGrid extends AdvancedDataGrid
{

	public function FooterAdvancedDataGrid()
	{
		super();
		doubleClickEnabled = true;
	}

	override protected function mouseDoubleClickHandler(event:MouseEvent):void
	{
		var dataGridEvent:AdvancedDataGridEvent;
		var r:IListItemRenderer;
		var dgColumn:AdvancedDataGridColumn;
		
		r = mouseEventToItemRenderer(event);
		try{
		if (r && r != itemEditorInstance)
		{
			var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
			if (columns[dilr.listData.columnIndex].editable)
			{
				dgColumn = columns[dilr.listData.columnIndex];
				dataGridEvent = new AdvancedDataGridEvent(AdvancedDataGridEvent.ITEM_EDIT_BEGINNING, false, true);
				// ITEM_EDIT events are cancelable
				dataGridEvent.columnIndex = dilr.listData.columnIndex;
				dataGridEvent.dataField = dgColumn.dataField;
				dataGridEvent.rowIndex = dilr.listData.rowIndex + verticalScrollPosition;
				dataGridEvent.itemRenderer = r;
				dispatchEvent(dataGridEvent);
			}
		}
		}catch(e:Error){}
		super.mouseDoubleClickHandler(event);
	}
	
	override protected function mouseUpHandler(event:MouseEvent):void
	{
		var r:IListItemRenderer;
		var dgColumn:AdvancedDataGridColumn;
		
		try{
		r = mouseEventToItemRenderer(event);
		if (r)
		{
			var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
			if (columns[dilr.listData.columnIndex].editable)
			{
				dgColumn = columns[dilr.listData.columnIndex];
				dgColumn.editable = false;
			}
		}
		
		super.mouseUpHandler(event);
		}catch(e:Error){}
		
		if (dgColumn)
			dgColumn.editable = true;
	}
	
    protected var footer:AdvancedDataGridFooter;

	protected var footerHeight:int = 22;

    override protected function createChildren():void
    {
        super.createChildren();

        if (!footer)
        {
            footer = new AdvancedDataGridFooter();
            footer.styleName = this;
            addChild(footer);
        }
    }

    override protected function adjustListContent(unscaledWidth:Number = -1,
                                       unscaledHeight:Number = -1):void
    {
		super.adjustListContent(unscaledWidth, unscaledHeight);

		listContent.setActualSize(listContent.width, listContent.height - footerHeight);
		
		footer.setActualSize(listContent.width, footerHeight);
		footer.move(listContent.x, listContent.y + listContent.height + 1);
	}

	override public function invalidateDisplayList():void
	{
		super.invalidateDisplayList();
		if (footer)
			footer.invalidateDisplayList();
	}

}

}